<?php
require_once "eventProvider.php";
require_once "eventSubscriber.php";
require_once "backendSQL.php";
require_once "user.php";

// Load all modules and make an instance of them.
function loadModules () {
	foreach (file(__DIR__."/../modules/loadorder", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $module) {
		if (strrpos($module,".php")) {
			require_once (__DIR__."/../modules/" . $module);
			$moduleInstanceName = (lcfirst(substr($module,0,-4)) . mt_rand(1000,9999));
			$moduleClassName = ucfirst(substr($module,0,-4));
			global $$moduleInstanceName;
			$$moduleInstanceName = new $moduleClassName($moduleInstanceName);		
		}
	}
}

// Locate the event provider or create one if it doesn't exist. 
// This is necessary because modules don't know whether it already exists.
function getEventProvider () {
	global $eventProvider;
	if (!($eventProvider instanceof EventProvider)) {
		$eventProvider = new EventProvider;
	}
	return $eventProvider;
}

function getDB () {
	global $db;
	if (!($db instanceof BackendSQL)) {
		$db = new BackendSQL('localhost','picturesque','root','');
	}
	return $db;
}

function getUser () {
	global $user;
	if (!($user instanceof User)) {
		$user = new User();
	}
	return $user;
}

function event ($event) {
	$ep = &getEventprovider();
	$ep->invokeEvent($event);
}

// Return the path to currently active style.
// Currently always returns default.
function getStylePath () {
	return (__DIR__."/../styles/picturesque");
}

// Get page contents from style path.
function getPageContents () {
	$pages = array("home", "feed", "user", "image","gallery");
	$compare = array_keys($_GET)[0];
	$stylepath = getStylePath();
	foreach ($pages as $page) {
		if ($compare == $page) {
			return ($stylepath . "/templates/" . $page . ".php");
		} 
	}
	return ($stylepath . "/templates/home.php");
}

function getNavItems () {
	return array("home", "feed", "user", "image","gallery");
}
?>